<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/28
 * Time: 11:40
 */
//error_reporting(E_ERROR | E_PARSE);


error_reporting(E_ALL);
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去
date_default_timezone_set('PRC');
define('ROOT_PATH', realpath(__DIR__).'/');
$controller = isset($argv[1]) ? $argv[1] : '';
$action = isset($argv[2]) ? $argv[2] : '';
if($controller && $action){
    try{
        require_once ROOT_PATH.'functions.php';
        spl_autoload_register('loadClass'); // Registers the autoloader
        //加载配置文件
        \lib\App::$config = require ROOT_PATH.'config.php';
        if(file_exists(ROOT_PATH.'config-local.php')) {
            \lib\App::$config = array_merge(\lib\App::$config, require ROOT_PATH.'config-local.php');
        }
        defined('ERROR_FILE') OR define('ERROR_FILE', ROOT_PATH.'err.log');
        //控制器
        $controllerClass = '\\controllers\\'.$controller;
        if(!class_exists($controllerClass)){
            throw new \Exception('controller is not exists.');
        }
        $obj = new $controllerClass();
        if(!method_exists($obj, $action)){
            throw new \Exception('method is not exists.');
        }
        //参数
        $param = isset($argv[3]) ? $argv[3] : '';
        if($param) {
            $_GET = [];
            foreach(explode('++', $param) as $item) {
                $tmp = explode('=', $item);
                $_GET[$tmp[0]] = $tmp[1];
            }
        }
        $result = $obj->$action();
        if($result) {
            exit( $result);
        } else {
            exit( 0);
        }
    } catch(\Exception $exception){
        echo $exception->getMessage()."\n";
        exit( 2);
    }
} else {
    echo 'args is invalid.';
}

